{% extends "base.html" %}
{% load navigation_tags %}
{% load authorization_tags %}
{% block content %}
    {{ block.super }}
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading tight">
                    <h3 class="has-filters">
                        {% if add_from_template %}
                            Add From Finding Templates
                        {% elif apply_template %}
                            Apply Template to Finding
                        {% else %}
                            Finding Templates
                        {% endif %}
                        <div class="dropdown pull-right">
                            <button id="show-filters" aria-label="Filters" data-toggle="collapse" data-target="#the-filters" class="btn btn-primary toggle-filters"> <i class="fa-solid fa-filter"></i> <i class="caret"></i> </button>
                            {% if "Finding_Add"|has_global_permission and not add_from_template and not apply_template %}
                                <a href="{% url 'add_template' %}" class="btn btn-primary"
                                   title="Add Finding Template">
                                    <i class="fa-solid fa-plus"></i>
                                </a>
                            {% endif %}
                            {% if add_from_template %}
                                <a href="{% url 'view_test' tid %}" class="btn btn-danger"
                                   title="Cancel add from template">
                                    <i class="fa-solid fa-ban"></i> Cancel
                                </a>
                            {% endif %}
                            {% if apply_template %}
                                <a href="{% url 'view_finding' fid %}" class="btn btn-danger"
                                   title="Cancel apply template">
                                    <i class="fa-solid fa-ban"></i> Cancel
                                </a>
                            {% endif %}
                            <button class="btn btn-primary download_templates" type="submit"
                                   title="Export to file" aria-label="Export to file">
                                    <i class="fa-solid fa-box-archive"></i>
                            </button>
                        </div>
                    </h3>
                </div>
                <div id="the-filters" class="is-filters panel-body collapse {% if filtered.form.has_changed %}in{% endif %}">
                    {% include "dojo/filter_snippet.html" with form=filtered.form %}
                </div>
            </div>
            {% if templates %}
                <div class="clearfix">
                    {% include "dojo/paging_snippet.html" with page=templates page_size=True %}
                </div>
                <div class="panel panel-default table-responsive">
                    <table id="template_findings"
                           class="tablesorter-bootstrap table table-condensed table-striped table-hover">
                        <thead>
                        <tr>
                            {% comment %} The display field is translated in the function. No need to translate here as well{% endcomment %}
                            <th nowrap="nowrap">{% dojo_sort request 'CWE' 'cwe' 'asc' %}</th>
                            <th nowrap="nowrap">Match Enabled</th>
                            {% comment %} The display field is translated in the function. No need to translate here as well{% endcomment %}
                            <th nowrap="nowrap">{% dojo_sort request 'Name' 'title' %}</th>
                            {% comment %} The display field is translated in the function. No need to translate here as well{% endcomment %}
                            <th nowrap="nowrap">{% dojo_sort request 'Severity' 'numerical_severity' %}</th>
                            <th nowrap="nowrap">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for finding in templates %}
                            <tr>
                                <td>
                                    {% if finding.cwe > 0 %}
                                        <a target="_blank"
                                           href="https://cwe.mitre.org/data/definitions/{{ finding.cwe }}.html">
                                            <i class="fa-solid fa-arrow-up-right-from-square"></i> {{ finding.cwe }}
                                        </a>
                                    {% endif %}
                                </td>
                                <td>{{ finding.template_match }}</td>
                                <td>
                                    {% if add_from_template %}
                                        <a href="{% url 'add_temp_finding' tid finding.id %}"
                                           class="template-popover" data-placement="auto bottom" data-toggle="popover"
                                           data-trigger="hover" title="{{ finding.title|truncatechars_html:100 }}"
                                           data-content="{{ finding.description|truncatechars_html:500 }}">
                                            {{ finding.title }}
                                            {% include "dojo/snippets/tags.html" with tags=finding.tags.all %}
                                        </a>
                                    {% elif apply_template %}
                                        <a href="{% url 'choose_finding_template_options' finding.id fid %}"
                                           class="template-popover" data-placement="auto bottom" data-toggle="popover"
                                           data-trigger="hover" title="{{ finding.title|truncatechars_html:100 }}"
                                           data-content="{{ finding.description|truncatechars_html:500 }}">
                                            {{ finding.title }}
                                            {% include "dojo/snippets/tags.html" with tags=finding.tags.all %}
                                        </a>
                                    {% else %}
                                        {% if "dojo.change_finding_template"|has_configuration_permission:request %}
                                        <a title="{{ finding.title }}" href="{% url 'edit_template' finding.id %}">
                                            {{ finding.title }}
                                            {% include "dojo/snippets/tags.html" with tags=finding.tags.all %}
                                        </a>
                                        {% else %}
                                            {{ finding.title }}
                                        {% endif %}
                                    {% endif %}
                                </td>
                                <td>{% if finding.severity == "Critical" or finding.severity == "High" %}
                                    <p class="text-error">
                                {% else %}<p>{% endif %}{{ finding.severity }}</p></td>
                                {% if add_from_template %}
                                    <td nowrap="nowrap">
                                        <a href="{% url 'add_temp_finding' tid finding.id %}"
                                           class="btn btn-success btn-sm">
                                            Use This Template
                                        </a>
                                    </td>
                                {% elif apply_template %}
                                    <td nowrap="nowrap">
                                        <a href="{% url 'choose_finding_template_options' finding.id fid %}"
                                           class="btn btn-success btn-sm">
                                            Use This Template
                                        </a>
                                    </td>
                                {% else %}
                                    <td nowrap="nowrap">
                                        {% if "Finding_Edit"|has_global_permission %}
                                        <a href="{% url 'edit_template' finding.id %}"
                                            class="btn btn-sm btn-secondary">Edit</a>
                                        {% endif %}
                                        {% if "Finding_Delete"|has_global_permission %}
                                        <form method="post" action="{% url 'delete_template' finding.id %}"
                                              style="display: inline" class="form-inline form">
                                            {% csrf_token %}
                                            <input type="hidden" name="id" value="{{ finding.id }}"/>
                                            <button type="submit" id="id_delete" class="btn btn-danger btn-sm template-delete">Delete
                                            </button>
                                        </form>
                                        {% endif %}
                                    </td>
                                {% endif %}
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
                <div class="clearfix">
                    {% include "dojo/paging_snippet.html" with page=templates page_size=True %}
                </div>
            {% else %}
                <p class="text-center">No finding templates found.</p>
            {% endif %}
        </div>
    </div>


{% endblock %}
{% block postscript %}
    {{ block.super }}
    <script>
        $(function () {
            $('button.template-delete').on('click', function (e) {
                return confirm('Are you sure you want to delete this template?')
            });

            $('[data-toggle="popover"]').popover();

            function JsonFileDownload() {
            fetch('template/export')
                .then(resp => resp.blob())
                .then(blob => {
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.style.display = 'none';
                a.href = url;
                // the filename you want
                a.download = 'FindingTemplates.json';
                document.body.appendChild(a);
                a.click();
                window.URL.revokeObjectURL(url);
                alert('All finding templates have successfully been exported and downloaded.');
            })
                .catch(() => alert('An error occurred when exporting the templates.'));
            }

            $('button.download_templates').on('click', function (e) {
                if (confirm('Do you wish to download these templates as a file?'))
                {
                    JsonFileDownload();
                }
            });

        });
    </script>
    {% include "dojo/filter_js_snippet.html" %}
{% endblock %}
